/**
 * 309. 最佳买卖股票时机含冷冻期
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
 */

/**
 * @param {number[]} prices
 * @return {number}
 */
function maxProfit(prices) {
  if (prices.length < 2) {
    return 0;
  }
  let preHold = -prices[0];
  let preNotHold = 0;
  let preNotHoldFrozen = 0;
  for (let i = 1; i < prices.length; i += 1) {
    const curHold = Math.max(preHold, preNotHold - prices[i]);
    const curNotHold = Math.max(preNotHold, preNotHoldFrozen);
    const curNotHoldFrozen = preHold + prices[i];
    preHold = curHold;
    preNotHold = curNotHold;
    preNotHoldFrozen = curNotHoldFrozen;
  }
  return Math.max(preNotHold, preNotHoldFrozen);
}

console.log(maxProfit([1, 2, 3, 0, 2]) === 3);
console.log(maxProfit([1]) === 0);
console.log(maxProfit([4, 2, 1]) === 0);
